document.getElementById("addMem").onclick=function(){
	window.open ("addUser.html?action=admin&type=POST", "_blank", "height=400, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
}
function deleteUser(e){
	var data={}
	data.username=e.getAttribute("username")
	var status=e.getAttribute("status")
	if (status==="true"){
		data.status=false
	}
	else{
		data.status=true
	}
	var _data=JSON.stringify(data)
	if(!confirm("真的要更改吗？")){
		return false;
	}
	$.ajax({
		"url":"/user_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"type":"DELETE",
		"complete":stop_load_pic,
		"data":{"data":_data},
		"success":function(return_data){
			if(responseCheck(return_data)){
				showSuccessNotice()
				e.setAttribute("status",data.status)
				console.log(data)
				if (data.status===true){
					e.className="btn btn-xs btn-success"
					e.textContent="正常"
					$(e).parent().next().find("button").attr("disabled",false)
				}
				else{
					e.className="btn btn-xs btn-danger"
					$(e).parent().next().find("button").attr("disabled",true)
				}
			}
		}
		
	})
	
}
function createLine(data){
				for(var i=0;i<data.content.length;i++){
					var tab=document.getElementById("tab")
					var tr=document.createElement("tr")

					var td=document.createElement("td")
					td.textContent=jiami(data.content[i].username,data.user_type)
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].chinese_name
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].oiler_code
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].site
					tr.appendChild(td)

					var td=document.createElement("td")
					if(data.content[i].user_type==="会员"){
						td.textContent=data.content[i].level+"星级会员"
					}
					else{
						td.textContent="--"
					}
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].member_code
					tr.appendChild(td)



					var td=document.createElement("td")
					if(data.content[i].user_type==="会员"){
						td.textContent=data.content[i].banance
					}
					else{
						td.textContent="--"
					}
					tr.appendChild(td)




					var td=document.createElement("td")
					td.textContent=data.content[i].number
					tr.appendChild(td)

					var td=document.createElement("td")
					var btn=document.createElement("button")
					var user_type=data.content[i].user_type
					if (user_type==="加油员"){
						btn.className="btn btn-success btn-xs"
					}
					else if(user_type==="管理员"){
						btn.className="btn btn-danger btn-xs"
					}
					else if(user_type==="财务"){
						btn.className="btn btn-primary btn-xs"
					}
					else if(user_type==="站长"){
						btn.className="btn btn-info btn-xs"
					}
					else if(user_type==="会员"){
						btn.className="btn btn-warning btn-xs"
					}
					btn.textContent=user_type
					td.appendChild(btn)
					tr.appendChild(td)

					var td=document.createElement("td")
					td.textContent=data.content[i].date
					tr.appendChild(td)

					var td=document.createElement("td")
					var b=document.createElement("button")
					b.setAttribute("username",data.content[i].username)
					b.setAttribute("status",data.content[i].status)
					b.onclick=function(){
						deleteUser(this)
					}
					if(data.content[i].status===false){
						b.className="btn btn-xs btn-danger"
						b.textContent="已停用"
					}
					else{
						b.className="btn btn-xs btn-success"
						b.textContent="正常"
					}
					
					td.appendChild(b)
					tr.appendChild(td)

					//
					var td=document.createElement("td")
					if(data.content[i].user_type==="加油员"){
						var b=document.createElement("button")
						b.className="btn btn-xs btn-warning"
						b.setAttribute("username",data.content[i].username)
						b.setAttribute("chinese_name",data.content[i].chinese_name)
						b.setAttribute("code",data.content[i].oiler_code)
						b.onclick=function(){
							var username=this.getAttribute("username")
							var oiler_code=this.getAttribute("code")
							var chinese_name=this.getAttribute("chinese_name")
							window.open ("code.html?p=" + username+"&chinese_name="+chinese_name+"&oiler_code="+oiler_code, "_blank", "height=600, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
						}
						b.textContent="生成二维码"
						if (data.content[i].status===false){
							b.setAttribute("disabled",true)
						}
						td.appendChild(b)
					}
					tr.appendChild(td)
		
					//
					var td=document.createElement("td")
					var b=document.createElement("button")
					b.textContent="编辑"
					b.className="btn btn-xs btn-primary"
					b.setAttribute("data",JSON.stringify(data.content[i]))
					b.onclick=function(){
						var data=this.getAttribute("data")
						window.open ("addUser.html?action=admin&type=PUT&data="+data, "_blank", "height=400, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
					}
					td.appendChild(b)

					var b=document.createElement("button")
					b.textContent="重置密码"
					b.style.marginLeft="5px"
					b.className="btn btn-xs btn-info"
					b.setAttribute("data",JSON.stringify(data.content[i]))
					b.onclick=function(){
						var data=this.getAttribute("data")
						data=JSON.parse(data)
						var username=data.username
						window.open ("password.html?username="+username, "_blank", "height=400, width=600, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
					}
					if(data.user_type==="财务"){
						td.appendChild(b)
					}
					tr.appendChild(td)
					tab.appendChild(tr)
			}
}
function getList(){
	$.ajax({
		"url":"/user_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
				createLine(data)
			}
		}
	})
}
document.getElementById("q1").onclick=function(){
	$("#username").val("")
	var data={}
	data.start_date=$("#start_date").val()
	data.end_date=$("#end_date").val()
	if(data.start_date==="" && data.end_date===""){
		showErrorInfo("请您选择一个日期进行查询！")
		return false;
	}
	data.user_type=$("#user_type").val()
	query(data)

}
document.getElementById("q2").onclick=function(){
	Q2()
}
function Q2(){
	$("#start_date").val("")
	$("#end_date").val("")
	var data={}
	data.username=$("#username").val()
	if(/^ *$/.test(data.username)){
		showErrorInfo("请输入会员号进行查询！")
		return false;
	}
	data.user_type=$("#user_type").val()
	query(data)
}
function query(data){
	$("#tab>tr").remove()
	data.action="admin"
	data=JSON.stringify(data)
	$.ajax({
		"url":"/user_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"data":data},
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
				createLine(data)
				var date1=$("#start_date").val()
				var date2=$("#end_date").val()
				if(date1==="" && date2 ==="") {
					if (data.today===undefined){
						//账户号码查询的情况
						$("#title").text("")
					}
					else{
						$("#title").text(data.today+" 至 现在")
					}
				}
				else{
					$("#title").text(date1 +" 至 "+date2)
				}
			}
		}
	})
}
document.getElementById("reset").onclick=function(){
	$("#tab>tr").remove()
	$("#start_date").val("")
	$("#end_date").val("")
	$("#username").val("")
}
//getList()

document.getElementById("username").focus()
document.getElementById("username").onkeyup=function(){
		if(event.keyCode==13){
			Q2()
		}
}
query({"start_date":"today","end_date":"","user_type":"全部"})
